import type { BaseController } from './definition'
import { POWER_STATES, OPERATION_STATES, DOOR_STATES } from '@/constants/Command'

export enum AsideRoles {
  Power = 'Power',
  // ChildLock = 'ChildLock',
}

export type Statements = ReturnType<typeof useStatements>
export function useStatements(store: BaseController.Store) {
  /**
   * 状态来源
   */
  // 离线
  const isOffline = computed(() => store.isOffline)
  // 本地操作是否开启
  const localOperating = computed(() => store.localOperating)
  // 远程控制是否开启
  const remoteControlDisabled = computed(() => !store.remoteControl)
  // 电源状态
  const isPowerOn = computed(() => store.powerState === POWER_STATES.ON)
  // 是否选择程序
  const programSelected = computed(() => store.currentProgramKey !== '')
  // 门状态
  const doorOpened = computed(() => store.doorState === DOOR_STATES.OPEN)
  // 远程启动
  const remoteStartDisabled = computed(() => !store.remoteStart)
  // 支持暂停程序
  const pauseSupported = computed(() => store.programPauseSupported)
  // 支持继续程序
  const resumeSupported = computed(() => store.programResumeSupported)
  // 支持停止程序
  const abortSupported = computed(() => store.programAbortSupported)
  /**
   * 页面元素状态
   */
  // 禁用全部状态
  const shouldDisableAll = computed(() => isOffline.value || localOperating.value || remoteControlDisabled.value)

  // 电源按钮
  const powerDisabled = computed(() => shouldDisableAll.value)
  // // 童锁
  // const childLockDisabled = computed(() => shouldDisableAll.value || doorOpened.value || remoteStartDisabled.value)
  const hasChildLock = computed(() => store.hasChildLock)
  const childLockOn = computed(() => store.childLockOn)
  const childLockActived = computed(() => hasChildLock.value && childLockOn.value)
  // 家电控制相关FlexLinks (除预约)
  const controlFlexLinksDisabled = computed(() => shouldDisableAll.value)
  // 选择程序
  const programSelectDisabled = computed(() => shouldDisableAll.value)
  // 调节参数
  const adjustParameterDisabled = computed(() => shouldDisableAll.value)
  // 预约 (入口置灰)
  const orderDisabled = computed(() => shouldDisableAll.value || !programSelected.value)
  // 预约 (选择时间置灰)
  const orderTimeDisabled = computed(
    () => orderDisabled.value || !store.currentProgram || !store.orderRelativeOption || childLockActived.value
  )
  // 取消预约
  const cancelOrderDisabled = computed(() => shouldDisableAll.value || !abortSupported.value || childLockActived.value)
  // 禁止启动程序
  const startDisabled = computed(
    () =>
      shouldDisableAll.value ||
      doorOpened.value ||
      !programSelected.value ||
      remoteStartDisabled.value ||
      childLockActived.value
  )
  // 禁止暂停程序
  const pauseDisabled = computed(() => shouldDisableAll.value || doorOpened.value || childLockActived.value)
  // 禁止继续程序
  const resumeDisabled = computed(() => shouldDisableAll.value || doorOpened.value || childLockActived.value)
  // 禁止停止程序
  const abortDisabled = computed(() => shouldDisableAll.value || doorOpened.value || childLockActived.value)

  /**
   * 是否显示控制页
   */
  const showControlView = computed(() => {
    const operationState = store.operationState || (store.detail?.operationState as OPERATION_STATES)
    /**
     * 离线 或 未加载操作状态时 显示程序列表页
     */
    if (isOffline.value || !operationState) return true

    /**
     * Ready Inactive Standby 状态时 显示程序列表页
     */
    const isListViewVisible = [
      // Inhouse 状态
      OPERATION_STATES.READY,
      OPERATION_STATES.INACTIVE,
      OPERATION_STATES.STANDBY,
      // OEM 状态
      OPERATION_STATES.OEM_BIWD_READY,
      OPERATION_STATES.OEM_BIWD_STANDBY,
    ].includes(operationState)
    return isListViewVisible
  })

  return {
    isOffline,
    localOperating,
    remoteControlDisabled,
    isPowerOn,
    showControlView,
    pauseSupported,
    resumeSupported,
    abortSupported,
    powerDisabled,
    // childLockDisabled,
    hasChildLock,
    childLockOn,
    childLockActived,
    controlFlexLinksDisabled,
    programSelectDisabled,
    adjustParameterDisabled,
    orderDisabled,
    orderTimeDisabled,
    cancelOrderDisabled,
    startDisabled,
    pauseDisabled,
    resumeDisabled,
    abortDisabled,
  }
}

export type ControlState = ReturnType<typeof useControlState>
export function useControlState(control: BaseController.Control) {
  return useStatements(control.store)
}
